package com.pay.ioopos.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.aggregate.pay.sanstar.SanstarApiFactory;
import com.aggregate.pay.sanstar.bean.PayRepealData;
import com.aggregate.pay.sanstar.bean.Result;
import com.aggregate.pay.sanstar.bean.TerminalPantData;
import com.aggregate.pay.sanstar.support.Client;
import com.aggregate.pay.sanstar.support.Merch;
import com.pay.ioopos.ApiFactory;
import com.pay.ioopos.DataFactory;
import com.pay.ioopos.DeviceUtils;
import com.pay.ioopos.Utils;
import com.pay.ioopos.activity.MainActivity;
import com.pay.ioopos.service.NetworkService;

public class PantReceiver extends BroadcastReceiver {
    private static final String TAG = PantReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        if (!intent.getAction().equals(PantReceiver.class.getName())) {
            return;
        }
        // 没连网或者没绑定
        String merchNo = DataFactory.getDataStore().getMerchNo();
        if (!NetworkService.isNetworkAvailable() || null == merchNo || merchNo.isEmpty()) {
            return;
        }
        try {
            startPant(context, intent);
        } catch (Exception e) {
            Log.d(TAG, "心跳异常", e);
            Toast.makeText(context, "心跳异常：" + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private void startPant(Context context, Intent intent) {
        TerminalPantData apiData = new TerminalPantData();
        apiData.setOnline(intent.getBooleanExtra("online", false));
        apiData.setAppVer(DeviceUtils.appVersion(context));
        apiData.setDevBrand(android.os.Build.BRAND);
        apiData.setDevImei(Utils.imei(context));
        apiData.setDevModel(android.os.Build.MODEL);
        apiData.setSysVer(android.os.Build.DISPLAY);
        /*
        String a = android.os.Build.BOARD;
        String b = android.os.Build.BOOTLOADER;
        String c = android.os.Build.BRAND;
        String d = android.os.Build.DEVICE;
        String e = android.os.Build.DISPLAY;
        String f = android.os.Build.FINGERPRINT;
        String g = android.os.Build.HARDWARE;
        String h = android.os.Build.HOST;
        String i = android.os.Build.ID;
        String j = android.os.Build.MODEL;
        String k = android.os.Build.MANUFACTURER;
        String l = android.os.Build.PRODUCT;
        String m = android.os.Build.RADIO;
        String n = android.os.Build.TAGS;
        */
        new Thread(() -> SanstarApiFactory.terminalPant(ApiFactory.initApi()).execute(apiData)).start();
    }
}
